<script lang="ts" setup>
import { ref, onMounted, watch } from "vue";
import json from "./assets/result.json";
import {
  getFeatureGroups,
  generateColorFromText,
} from "./libs/jsonParser";
import Scene from './libs/scene';

const features = getFeatureGroups(json);
const CANVAS_SIZE = 900;

const groupKeys = Object.keys(features);
const colors = {};
groupKeys.forEach((key) => {
  colors[key] = generateColorFromText(key);
});

const checkedGroup = ref([...groupKeys]);

onMounted(() => {
  const cvs = document.querySelector("#cvs") as HTMLCanvasElement;
  const scene = new Scene(cvs, CANVAS_SIZE, checkedGroup, colors);

  scene.updateCanvas(features);

  watch(checkedGroup, () => {
    scene.updateCanvas(features);
  });
});
</script>

<template>
  <div class="panel" :style="{ '--size': `${CANVAS_SIZE}px` }">
    <div class="panel__draw">
      <canvas id="cvs"></canvas>
    </div>

    <div class="panel__check">
      <div v-for="key in groupKeys" :key="key">
        <input
          type="checkbox"
          name="check"
          :id="key"
          :value="key"
          v-model="checkedGroup"
        />
        <label :for="key">{{ key }}</label>
        <span
          class="panel__check__color"
          :style="{ backgroundColor: colors[key] }"
        ></span>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  border-radius: 3px;
  background: rgba(0, 0, 0, 0.06);
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.08);
}
/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
  border-radius: 3px;
  background: rgba(0, 0, 0, 0.12);
  -webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.2);
}

.panel {
  height: 100vh;
  display: grid;
  grid-template-columns: minmax(var(--size), 1fr) 300px;
  place-items: center;
  &__draw {
    display: flex;
    justify-content: center;
    align-items: center;
    canvas {
      border: 2px dashed #d0d0d0;
      width: var(--size);
      height: var(--size);
    }
  }
  &__check {
    height: 90vh;
    overflow-y: scroll;
    input {
      margin-right: 0.5rem;
    }
    &__color {
      display: inline-block;
      width: 10px;
      height: 10px;
      margin-left: 0.5rem;
    }
  }
}
</style>
